<template>
    <div>
        <Modal v-model="isShowModal" :title="title" @on-cancel="hide('formValidate')" :mask-closable="false">


            <Form ref="formValidate" :model="params" :rules="ruleValidate" :label-width="80">
                <FormItem label="账号：" prop="username">
                    <Input maxlength="128" v-model.trim="params.username" :disabled="usernameDis" placeholder="请输入账号"></Input>
                </FormItem>
                <FormItem label="姓名：" prop="realName">
                    <Input v-model.trim="params.realName" placeholder="请输入姓名"></Input>
                </FormItem>
                <FormItem label="权限组：" prop="roleId">
                    <Select v-model="params.roleId" placeholder="选择权限组">
                        <Option v-for="item in roleList" :value="item.adminRoleId" :key="item.adminRoleId">
                            {{item.roleName}}
                        </Option>
                    </Select>
                </FormItem>
            </Form>


            <!-- 重写对话框底部按钮 -->
            <div slot="footer">
                <Button @click="hide('formValidate')">取消</Button>
                <Button v-if="type != 1" type="primary" @click="handleSubmit('formValidate')" :loading="loadingBtn">
                    <span v-if="!loadingBtn">确定</span>
                    <span v-else>请稍候...</span>
                </Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    export default {
        name: "admin-ope",
        props: ['roleList'],
        data() {
            return {
                isShowModal: false,
                loadingBtn: false,
                usernameDis: false,
                title: '查看信息',
                type: 0,
                params: {
                    username: '',
                    realName: '',
                    roleId: '',
                    adminId:''
                },
                ruleValidate: {
                    username: [
                        {required: true, message: '账号不能为空', trigger: 'change'},
                        {max: 128, message: '账号名称过长', trigger: 'change'}
                    ],
                    realName: [
                        {required: true, message: '姓名不能为空', trigger: 'change'},
                        {max: 20, message: '账号名称过长', trigger: 'change'}
                    ],
                    roleId: [
                        {required: true, message: '请选择权限组', trigger: 'change', type: 'number'}
                    ]
                }
            }
        },
        methods: {
            // 点击提交验证
            handleSubmit(name) {
                // console.log(this.params);
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        // this.$Message.error('验证通过');
                        /**
                         * type判断
                         * 1.查看 2.新增 3.编辑
                         */
                        switch (this.type) {
                            case 1:
                                // this.title = '查看管理员';
                                break;
                            case 2:
                                // 新增管理员
                                this.addAdmin();
                                break;
                            case 3:
                                // 编辑管理员
                                this.updateAdmin();
                                break;
                            default:
                                this.$Notice.error({
                                    title: '管理员管理',
                                    desc: '查看，新增，编辑区分判断报错',
                                    duration: 0
                                });
                                break
                        }
                    } else {
                        this.$Message.error('还有信息没有填写合格哦~');
                    }
                })
            },
            // 显示模态框
            show(type, row) {
                this.isShowModal = true;
                this.type = type;
                /**
                 * type判断
                 * 1.查看 2.新增 3.编辑
                 */
                switch (type) {
                    case 1:
                        this.title = '查看管理员';
                        this.findAdminUserById(row);
                        break;
                    case 2:
                        this.title = '新增管理员';
                        break;
                    case 3:
                        this.title = '编辑管理员';
                        this.findAdminUserById(row);
                        break;
                    default:
                        this.$Notice.error({
                            title: '管理员管理',
                            desc: '查看，新增，编辑模态框报错',
                            duration: 0
                        });
                        break
                }
            },
            // 新增管理员
            addAdmin() {
                this.$Spin.show();
                this.axios.post('/adminUser/addAdmin', this.params).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.$emit('changeDate');
                        this.$Message.success('操作成功.');
                        this.hide('formValidate');
                    }
                    if(res.status == 201){
                        this.$Message.error(res.msg);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '新增管理员接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 编辑管理员
            updateAdmin(){
                this.$Spin.show();
                this.axios.post('/adminUser/updateAdmin', this.params).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.$emit('changeDate');
                        this.$Message.success('操作成功.');
                        this.hide('formValidate');
                    }
                    if(res.status == 201){
                        this.$Message.error(res.msg);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '编辑管理员接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 查询管理员详情
            findAdminUserById(row){
                this.$Spin.show();
                this.axios.post('/adminUser/findAdminUserById', {
                    adminId: row.adminId
                }).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        // this.$Message.success('操作成功.');
                        this.params.username = res.data.username;
                        this.params.realName = res.data.realName;
                        this.params.roleId = res.data.roleId;
                        this.params.adminId = res.data.adminId;
                        this.usernameDis = true;
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '查看管理员详情接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 关闭模态框
            hide(name) {
                this.isShowModal = false;
                this.params.adminId = '';
                this.usernameDis = false;
                this.$refs[name].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>